global class HandlingAttachments{
    static String testVar = '';

    WebService static String retreiveContentType(String salesContractAttachmentId)
    {
        System.LoggingLevel level = LoggingLevel.DEBUG;
    
        Attachment[] a = [SELECT Id, ContentType FROM Attachment a where a.Id = :salesContractAttachmentId];
    
        if(a.size() == 0)
            return 'null';
    
        return a[0].ContentType;
    }

  WebService static String retreiveBody(String salesContractAttachmentId){
    Attachment[] a = [SELECT Id, Body FROM Attachment a where a.Id = :salesContractAttachmentId];
    
    if(a.size()==0)
        return 'null';
        
    Blob blobContent = a[0].Body;

    String body = blobContent.toString();
     
    return body;
  }
  
	//finds whether a Template has Attachments are not.   
	Webservice static Boolean hasAttachments(ID templateId)
	{
	 	Attachment[] att = [select Id from Attachment where parentid = :templateId];
	 	if(att.size() > 0)
	 		return true;
	 	return false;
	}//end of hasAttachments(ID)

  WebService static String updateStatus(String salesContractId){
    Sales_Contract__c[] sc = [SELECT Status__c FROM Sales_Contract__c s where s.Id = :salesContractId];
    if(sc.size() == 0)
        return 'null';
    else
    {
        sc[0].Status__c = 'Viewed';
        sc[0].DocMerged__c=true;
        sc[0].DateViewed__c=System.now();
        
        if(testVar != '' && testVar.length() > 0)
        {
            sc[0].Level1__c = '005400000011epc';
            sc[0].Level2__c = '005400000011epc';
            sc[0].Level3__c = '005400000011epc';
            sc[0].Level4__c = '005400000011epc';
            sc[0].Level5__c = '005400000011epc';
            sc[0].Level6__c = '005400000011epc';
            sc[0].Level7__c = '005400000011epc';
            sc[0].Level8__c = '005400000011epc';
            sc[0].Level9__c = '005400000011epc';
            sc[0].Level10__c = '005400000011epc';
            sc[0].Level11__c = '005400000011epc';
            sc[0].Level12__c = '005400000011epc';
            sc[0].Level13__c = '005400000011epc';
            sc[0].Level14__c = '005400000011epc';
            sc[0].Level15__c = '005400000011epc';
            sc[0].Final_Approver__c = '005400000011epc';
            sc[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
        }

        if(testVar == 'Published')
        {
            sc[0].Status__c = 'Published';
        }
        if(testVar == 'Plan Approved 1')
            sc[0].Status__c = 'Plan Approved by Engineering Callidus';
        if(testVar == 'Plan Rejected 1')
            sc[0].Status__c = 'Plan Rejected by Engineering Callidus';

        if(testVar != '' && testVar.length() > 0)
        {
            sc[0].Level1__c = '005400000011eqL';
            sc[0].Level2__c = '005400000011eqL';
            sc[0].Level3__c = '005400000011eqL';
            sc[0].Level4__c = '005400000011eqL';
            sc[0].Level5__c = '005400000011eqL';
            sc[0].Level6__c = '005400000011eqL';
            sc[0].Level7__c = '005400000011eqL';
            sc[0].Level8__c = '005400000011eqL';
            sc[0].Level9__c = '005400000011eqL';
            sc[0].Level10__c = '005400000011eqL';
            sc[0].Level11__c = '005400000011eqL';
            sc[0].Level12__c = '005400000011eqL';
            sc[0].Level13__c = '005400000011eqL';
            sc[0].Level14__c = '005400000011eqL';
            sc[0].Level15__c = '005400000011eqL';
            sc[0].Final_Approver__c = '005400000011eqL';
            sc[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
        }
        if(testVar == 'Plan Approved 2')
            sc[0].Status__c = 'Plan Approved by Jack Hill';
        if(testVar == 'Plan Rejected 2')
            sc[0].Status__c = 'Plan Rejected by Jack Hill';

        try{
          update sc;
        }
        catch(DmlException d)
        {
         
        }
        Sales_Contract__History history = new Sales_Contract__History(ParentId = salesContractId);
    }
   
    return 'Updated Successfully';    
  }
  
    static testMethod void testHelloWorld() 
    {
        testVar = 'Published';
        updateStatus('a0E40000001lklwEAA');
        testVar = 'Plan Approved 1';
        updateStatus('a0E40000001lklwEAA');
        testVar = 'Plan Rejected 1';
        updateStatus('a0E40000001lklwEAA');
        testVar = 'Plan Approved 2';
        updateStatus('a0E40000001lklwEAA');
        testVar = 'Plan Rejected 2';
        updateStatus('a0E40000001lklwEAA');    

        updateStatus('a0E40000001lklwEA0');//Wrong Id 

        
        HandlingAttachments.retreiveBody('00P40000002PvE2');
        HandlingAttachments.retreiveBody('00P40000002PvEY');//Wrong Id
        HandlingAttachments.retreiveContentType('00P40000002PvE2');
        HandlingAttachments.retreiveContentType('00P40000002PvEY');//Wrong Id
//        HandlingAttachments.updateStatus('a0E40000001lklwEAA');
    }


}